package com.CityHunter.MapActivity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.CityHunter.FriendList;
import com.CityHunter.R;
import com.CityHunter.Track;
import com.CityHunter.Activity.CameraView;
import com.CityHunter.OverLay.FriendOl;
import com.CityHunter.OverLay.MapOl;
import com.CityHunter.OverLay.TrackOverLay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ShowTrack extends MapActivity {
	
	GeoPoint gp;
	MapView mview;
	MapController mc;
	LocationManager lm;
	List<Overlay> mapOverlays;
	
	Location loc;
	FriendOl fol;
	SharedPreferences sp;
	MapOl mo;
	String grouptitle;
	FriendList fl = new FriendList();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		String serviceName = Context.LOCATION_SERVICE;
		lm = (LocationManager) getSystemService(serviceName);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		String provider = lm.getBestProvider(criteria, true);
		loc = lm.getLastKnownLocation(provider);
		mview = (MapView) findViewById(R.id.mapview);
		mc = mview.getController();
		mc.setZoom(15);
		mview.setBuiltInZoomControls(true);
		mapOverlays = mview.getOverlays();

		
		
		centerOnGPSPosition();

	//	 lm.requestLocationUpdates(provider, 0, 0, locationListener);


	}
	public boolean onCreateOptionsMenu(Menu menu) {
	       
        menu.add(0, 0, 0, "Realtity View");
      menu.add(0, 1, 1, "Exit");
    return super.onCreateOptionsMenu((android.view.Menu) menu);
   }
     
    public boolean onOptionsItemSelected(MenuItem item) {
 
       switch(item.getItemId()) {
           case 0:
              
        	   Intent intent = new Intent(ShowTrack.this,CameraView.class);
				intent.putExtra("grouptitle", grouptitle);
				startActivityForResult(intent, 1);
            case 1:
             
                break;
            default:
        }
    return super.onOptionsItemSelected(item);
    }
	public void centerOnGPSPosition() {

		gp = new GeoPoint((int) (loc.getLatitude() * 1000000), (int) (loc
				.getLongitude() * 1000000));

		mc.animateTo(gp);
		mc.setCenter(gp);
		// show Overlay on map.
		mo = new MapOl(gp);
	
	//	fol = new FriendOl(fl);

		mapOverlays.add(mo);
		Track track=new Track("Testing","");
		track.addPointByLatLong(22.352219,114.120076);
		track.addPointByLatLong(22.349619,114.113939);
		track.addPointByLatLong(22.34692,114.108253);
		mapOverlays.add(new TrackOverLay(track));
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	

	/*
	 * 
	 * public class mainTask extends TimerTask { public void run() {
	 * 
	 * 
	 * 
	 * mapOverlays.clear(); mapOverlays.add(mo);
	 * 
	 * fol = new FriendOl(sp.getString("name", ""));
	 * 
	 * 
	 * mapOverlays.add(fol);
	 * 
	 * 
	 * // upload(); } }
	 * 
	 * public void upload() { ArrayList<NameValuePair> nameValuePairs = new
	 * ArrayList<NameValuePair>();
	 * 
	 * nameValuePairs.add(new BasicNameValuePair("name", sp.getString("name",
	 * ""))); nameValuePairs.add(new BasicNameValuePair("lat",
	 * String.valueOf(loc .getLatitude()))); nameValuePairs.add(new
	 * BasicNameValuePair("long", String.valueOf(loc .getLongitude()))); try {
	 * 
	 * HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new
	 * HttpPost( "http://fypgps.hyperphp.com/update.php");
	 * httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	 * httpclient.execute(httppost);
	 * 
	 * } catch (Exception e) {
	 * 
	 * } }
	 */
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {

			gp = new GeoPoint((int) (location.getLatitude() * 1000000),
					(int) (location.getLongitude() * 1000000));
			mc.animateTo(gp);
			mc.setCenter(gp);
			mapOverlays.clear();
			mapOverlays.add(mo);

			 fol = new FriendOl(fl);

			 mapOverlays.add(fol);

		}

		public void onProviderDisabled(String provider) {

		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};
}
